from tkinter import Tk, Button, Label, Text, END
from tkinter import filedialog
import tkinter.messagebox as msgbox
import metapensiero.pj
from os import path, system
from time import sleep
from sys import exit


class Application(object):
    def __init__(self):
        self.window = Tk()
        self.window.title(u'Box3PythonCompiler 0.1')
        self.window.geometry('310x150+500+300')
        self.window.minsize(310, 150)
        self.window.maxsize(310, 150)
        self.result_text1 = Text(self.window)
        self.title_label2 = Label(
            self.window, text=u'python文件目录:(请在按钮选取，此处填写无效')
        self.title_label2.place(x=10, y=5)
        self.result_text3 = Text(self.window)
        self.result_text3.place(x=10, y=25, width=285, height=30)
        self.submit_btn = Button(
            self.window, text=u'开始转换', command=self.submit)
        self.submit_btn.place(x=10, y=55, width=110, height=35)
        self.submit_btn2 = Button(
            self.window, text=u'选择python文件目录', command=self.clean)
        self.submit_btn2.place(x=150, y=55, width=145, height=35)
        self.title_label4 = Label(
            self.window, text=u'生成的js文件也会在选择的python文件的目录下\n文件名不能包括中文')
        self.title_label4.place(x=10, y=95)

    def clean(self):
        global dir_path
        dir_path = filedialog.askopenfilename()
        #dir_path = dir_path.replace("/", "\\\\")
        self.result_text3.delete(0.0, END)
        self.result_text3.insert('end', dir_path)

    def run(self):
        self.window.mainloop()

    def submit(self):
        if dir_path != '':
            namen = path.split(dir_path)[0]
            namend = path.splitext(path.split(dir_path)[1])[1]
            if namend == '.py':
                '''if is_chinese(path.splitext(path.split(dir_path)[1])[0]):
                    print('抱歉，文件名不能包含中文')
                else:'''

                msgbox.showinfo('准备转换，请继续并等待一段时间(大约十秒)')
                print('python -m metapensiero.pj ' + dir_path)
                system('python -m metapensiero.pj ' + dir_path)
                sleep(5)

                f = open(
                    namen+'/'+path.splitext(path.split(dir_path)[1])[0]+'.js', "r+")
                if f.read():
                    msgbox.showinfo('你成功了，请根据说明文档进行下一步操作')
                    '''app = open(
                                    namen+'/'+path.splitext(path.split(dir_path)[1])[0]+'.js')
                        for line in app.readlines():
                            # line = line.strip("\n")
                            if 'import' in line or 'voxel = voxels();' in line or 'entity = entitys();'in line or 'player = entitys().players();'in line or 'world = worlds();'in line or 'console = consoles();':
                                app.readlines()[line] = ''
                        app.close()'''
                    a = '''
//这段代码必须转换之后放到代码中，否则会报错
world.onPlayerJoin(({entity})=>{
    player = entity.player
})

'''+f.read()
                    f.write(a)
                    f.close()

            else:
                print('错误提示', '选择的文件必须是.py文件')
        else:
            print('错误提示', '请选择路径')

        # print(namen)
app = Application()
app.run()
